<gm:page title="test" authenticate="false" onload="init();" >

  
<!--  DATA SOURCES  -->  
<gm:list id="emptyFeed" data="${tmp}/empty"/>
  
<input type="button" value="Reload" onClick="pipe();"/> 
<input type="button" value="Delete" onClick="remove();"/> 
<br />

<div id="searchBox" onfocus="alert('aa');"> 
<gm:search id="mySearch" data="${base}" hint="Ex. 'fácil' ou 'cachoeira'">
      <gm:param name="item type" value="Hike Reviews"/>
  <gm:handleEvent event="all" execute="alert(event.type)"/>       
</gm:search>
     <button id="clearSearch" onClick="init();">Limpar</button>  
      <button id="sss" onClick="deselect();">deselect</button>  
</div>

<br />
<gm:list id="myList" data="${mySearch}" template="base">
  <gm:handleEvent event="repaint" execute="pipe();"/> 
</gm:list>  
 

<br />
<gm:list id="tmpFeed" data="${tmp}/mainfeed" template="myListPipedTemplate"/>
 

<!-- ========================== HTML Templates ============================ -->

<!-- Lista de Trilhas -->  
<gm:template id="myListTemplate">
  <div repeat="true">
    <gm:text ref="atom:title"/> 
  </div>      
</gm:template>  

<gm:template id="myListPipedTemplate">
  <table>
    <tr repeat="true">
      <td><gm:rating ref="gd:rating"/></td><td> <gm:text ref="atom:title"/> </td>
   </tr>
  </table>   
</gm:template>  


<gm:template id="base">
  <div repeat="true">
   <gm:text ref="atom:title"/> 
  </div>   
</gm:template>  


<!-- ================================ JavaScript Code ============================== -->
<script>

function init() { 
    var search = google.mashups.getObjectById("mySearch");
    search.clear();
    search.submit();     
}   
  
function deselect() { 
  var list =  google.mashups.getObjectById('tmpFeed');  
  list.selectEntry("");
}   
      
function remove() {
  var destData = google.mashups.getObjectById('tmpFeed').getData();  
  //var listEntry = google.mashups.getObjectById('tmpFeed').getSelectedEntry();
  
  var i = 0;
  for (i = destData.size() - 1; i >= 0; i--) {  
     destData.removeEntry(destData.entryAt(i));   
  }
}  

function pipe() {
  var srcData =  google.mashups.getObjectById('myList').getData();   
  var destData =  google.mashups.getObjectById('tmpFeed').getData();   
  //var eData =  google.mashups.getObjectById('emptyFeed').getData();   
  //google.mashups.getObjectById('tmpFeed').setData("${tmp}/empty");
  
  // Empty Dest
  //alert("dest: " + destData.size());
  var i = 0;
  var entry;
  for (i = destData.size() - 1; i >= 0; i--) {  
     try { 
       entry = destData.entryAt(i);
       //alert(entry);
       destData.removeEntry(destData.entryAt(i)); 
     } catch(err)   {
        alert("error:" + err.description);
      }  
  }  
  //alert("dest depois: " + destData.size());  
  //alert("src: " + srcData.size());
  for (i = 0; i < srcData.size(); i++) {
    var oldEntry = srcData.entryAt(i);  
    var entry = GPath.cloneEntry(oldEntry) 
  
    // Create Rating
    ratingGPath = new GPath("g:rating");  
    srcValue = ratingGPath.getValue(entry);  
  
    valueGPath = new GPath("gd:rating/@value"); 
    minGPath = new GPath("gd:rating/@min");
    maxGPath = new GPath("gd:rating/@max");   
    averageGPath = new GPath("gd:rating/@average");     

    valueGPath.setValue(entry, srcValue);  
    averageGPath.setValue(entry, srcValue);
    minGPath.setValue(entry, "1");  
    maxGPath.setValue(entry, "5");    
    
    try { 
     destData.addEntry(entry);    
    } catch(err)   {
      alert("error:" + err.description);
    }
  }
  //google.mashups.getObjectById('tmpFeed').setData("${tmp}/mainfeed");
  //google.mashups.getObjectById('tmpFeed').refresh();
}    
 

</script>
  
  
</gm:page>